package com.hutiao.snowhb.actions.mm;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.hutiao.snowhb.common.WeixinUtil;
import com.hutiao.snowhb.entities.SnowMM;
import com.hutiao.snowhb.entities.SnowMMComment;
import com.hutiao.snowhb.entities.SnowMMLocation;
import com.hutiao.snowhb.entities.SnowMMPrebook;
import com.hutiao.snowhb.entities.WeixinUserInfo;
import com.hutiao.snowhb.service.SnowMMService;
import com.hutiao.snowhb.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/snowmm")
public class SnowMMAction extends ActionSupport {
	
	private static final long serialVersionUID = -8920240306520024315L;
	
	private static Logger logger = Logger.getLogger(SnowMMAction.class);
	
	@Autowired
	private SnowMMService mmService;
	
	@Autowired
	private UserService userService;
	
	private List<SnowMM> mmList;
	
	private SnowMM mmDetail;
	
	private int mmid;
	
	private List<SnowMMPrebook> bookList;
	
	private String visitorId;
	
	private WeixinUserInfo visitor;

	public List<SnowMM> getMmList() {
		return mmList;
	}

	public void setMmList(List<SnowMM> mmList) {
		this.mmList = mmList;
	}

	public SnowMM getMmDetail() {
		return mmDetail;
	}

	public void setMmDetail(SnowMM mmDetail) {
		this.mmDetail = mmDetail;
	}

	public int getMmid() {
		return mmid;
	}

	public void setMmid(int mmid) {
		this.mmid = mmid;
	}
	
	public List<SnowMMPrebook> getBookList() {
		return bookList;
	}

	public void setBookList(List<SnowMMPrebook> bookList) {
		this.bookList = bookList;
	}

	public String getVisitorId() {
		return visitorId;
	}

	public void setVisitorId(String visitorId) {
		this.visitorId = visitorId;
	}

	public WeixinUserInfo getVisitor() {
		return visitor;
	}

	public void setVisitor(WeixinUserInfo visitor) {
		this.visitor = visitor;
	}

	@Action(value="mm_list")
	public String mmListPage() {
		
		this.mmList = this.mmService.getMMLocaList();
		
		return SUCCESS;
	}
	
	@Action(value="mm_detail")
	public String mmDetailPage() throws Exception {
		
		this.visitor = this.userService.getWxUserInfo(this.visitorId);
		
//		if (this.visitor == null) {
//			this.visitor = WeixinUtil.getUserInfo(this.visitorId);
//			// 更新或新建用户微信资料
//			this.userService.saveUser(this.visitor);
//		}
		
		this.mmDetail = this.mmService.getMMById(this.mmid);
		
		List<SnowMMComment> comments = this.mmService.getComments(this.mmid);
		this.mmDetail.setComments(comments);
		
		SnowMMLocation loca = this.mmService.getCurrentLocation(this.mmid);
		this.mmDetail.setCurrLocation(loca);
		
		return SUCCESS;
	}
	
	@Action(value="iamsnowmm")
	public String iamsnowmm() {
		
		this.mmDetail = this.mmService.getMMById(this.mmid);
		
		this.bookList = this.mmService.getPrebookList(mmid, 0);
		
		return SUCCESS;
	}
	
}
